package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Hashtable;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;

public class Favoritos extends Activity {

	private ImageView imagen;
	private TextView titulo;
	private Dictionary dic;
	public ArrayList<Dictionary> Actividades;
	public ListView list;
	public ListAdapter adapter;
	public Dialog progressDialog;
	public App_ImageLoader imageLoader;
	private Intent intent;
	private int filtro = 0;

	private boolean put_filter = false;
	private String put_qry;
	protected String QRY_BUSCADOR;
	public Integer EVEIDR;
	
	public static ArrayList<Dictionary<String,String>> arrPatrocinadores = null;
	public static ArrayList<Dictionary<String,String>> arrExpositores = null;
	public static ArrayList<Dictionary<String,String>> arrPonencias = null;
	public static ArrayList<Dictionary<String,String>> arrPonentes = null;
	public static ArrayList<Dictionary<String,String>> arrCupones = null;
	public static ArrayList<Dictionary<String,String>> arrEventos = null;
	public static ArrayList<Dictionary<String,String>> arrNoticias = null;
	public static ArrayList<Dictionary<String,String>> arrGeneral = null;
	public static ArrayList<Dictionary<String,String>> Splash = null;

	Utilities util;
	
	public void voidFinish(View V) {
		finish();
	}

	
	public void abrirMenuLateral(View V) {
		intent = new Intent(Favoritos.this, MenuLateral.class);
		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}
	
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig); 
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_favoritos);
		EVEIDR = Utilities.EVE_IDR;
		util = new Utilities();

		//splash
		try{
			String qry = "select a.* from eventos_banners a inner join banners_catalogo b on a.ban_idr = b.ban_idr where a.eve_idr = "+EVEIDR+" and b.ban_nombre = 'SPLASH FAVORITOS' ORDER BY b.ban_nombre";
			Splash = Utilities.qry2(getApplicationContext(), qry);
			
	    	if(Splash.size()>0){
	    		Dictionary<String, String> dic = Splash.get(0);
		    	String strPoster = dic.get("ban_url").toString();
			    String strTimer = dic.get("ban_timer").toString();
			    String strPatrocinador = dic.get("pat_idr").toString();
			    if (strPoster.equals("null")) {
			        strPoster = "";
			    }
			    if(strPoster.length()>0){
					intent = new Intent(this, Splash2.class);
					Bundle bund = new Bundle();
					bund.putString("PUT_IMG",strPoster);
					bund.putString("PUT_PAT",strPatrocinador);
					bund.putString("PUT_TIME",strTimer);
					intent.putExtras(bund);
					startActivity(intent);
			    }
		    }
		}catch(Exception ex){}
		
		CargarContenido();
	}


	private void CargarContenido() {
		new AsyncActividades().execute("contenido");
	}
	

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {
		protected void onPostExecute(Integer in) {
			Resources res = getResources();
			ListView listView = (ListView) findViewById(id.list);
			FavoritosArrayAdapter adapter = new FavoritosArrayAdapter(Favoritos.this, arrGeneral, res.getString(R.string.QRY_SERVLET));
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
					muestra_detalle(position);
				}
			});
			progressDialog.dismiss();
		}

		protected void onPreExecute() {
			progressDialog = ProgressDialog.show(Favoritos.this, "", "Cargando...");
		}

		protected Integer doInBackground(String... urls) {
		    String strSQL = util.getFavoritos(Favoritos.this,"PATROCINADOR");
		    if (strSQL.length()>0) {
		        strSQL = "select b.* from patrocinadores_eventos as a left outer join patrocinadores as b on a.pat_idr = b.pat_idr where a.eve_idr = "+EVEIDR+" and  b.pat_idr in("+strSQL+") order by pat_nombre";
		        arrPatrocinadores = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		        arrPatrocinadores = new ArrayList<Dictionary<String,String>>();
		    }
			
		    strSQL = util.getFavoritos(Favoritos.this,"EXPOSITOR");
		    if (strSQL.length()>0) {
		        strSQL = "select * from expositores where eve_idr = "+EVEIDR+" and exp_idr in("+strSQL+") order by exp_nombre";
		        arrExpositores = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		    	arrExpositores = new ArrayList<Dictionary<String,String>>();
		    }
		    
		    strSQL = util.getFavoritos(Favoritos.this,"PONENCIA");
		    if (strSQL.length()>0) {
		        strSQL = "select * from ponencias where eve_idr = "+EVEIDR+" and nen_idr in("+strSQL+") order by nen_fecha,nen_inicio,nen_titulo";
		        arrPonencias = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		    	arrPonencias = new ArrayList<Dictionary<String,String>>();
		    }
		    
		    strSQL = util.getFavoritos(Favoritos.this,"PONENTE");
		    if (strSQL.length()>0) {
		        strSQL = "select * from ponentes where eve_idr = "+EVEIDR+" and pon_idr in("+strSQL+") order by pon_nombre,pon_paterno,pon_materno";
		        arrPonentes = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		    	arrPonentes = new ArrayList<Dictionary<String,String>>();
		    }
		    
		    strSQL = util.getFavoritos(Favoritos.this,"CUPON");
		    if (strSQL.length()>0) {
		        strSQL = "select a.*, poi_thumb from cupones a left outer join pois b on a.poi_idr = b.poi_idr where a.eve_idr = "+EVEIDR+" and a.cup_idr in("+strSQL+") order by a.cup_promocion";
		        arrCupones = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		    	arrCupones = new ArrayList<Dictionary<String,String>>();
		    }
		    
		    strSQL = util.getFavoritos(Favoritos.this,"EVENTOSOC");
		    if (strSQL.length()>0) {
		        strSQL = "select * from eventos_sociales where eve_idr = "+EVEIDR+" and evs_idr in("+strSQL+") order by evs_titulo";
		        arrEventos = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		    	arrEventos = new ArrayList<Dictionary<String,String>>();
		    }
			
		    strSQL = util.getFavoritos(Favoritos.this,"NOTICIA");
		    if (strSQL.length()>0) {
		        strSQL = "select * from noticias where not_idr in("+strSQL+") order by not_fecha";
		        arrNoticias = Utilities.qry2(getApplicationContext(), strSQL);
		    }else{
		    	arrNoticias = new ArrayList<Dictionary<String,String>>();
		    }
			
		    //Junto todo en un solo arreglo
		    Dictionary<String, String> dic = null;
		    arrGeneral = new ArrayList<Dictionary<String,String>>();
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_patrocinios));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrPatrocinadores.size();i++){
		    	dic = arrPatrocinadores.get(i);
		    	dic.put("tipoF","Patrocinadores");
		    	arrGeneral.add(dic);
		    }
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_expositores));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrExpositores.size();i++){
		    	dic = arrExpositores.get(i);
		    	dic.put("tipoF","Expositores");
		    	arrGeneral.add(dic);
		    }
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_programa));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrPonencias.size();i++){
		    	dic = arrPonencias.get(i);
		    	dic.put("tipoF","Ponencias");
		    	arrGeneral.add(dic);
		    }
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_ponentes));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrPonentes.size();i++){
		    	dic = arrPonentes.get(i);
		    	dic.put("tipoF","Ponentes");
		    	arrGeneral.add(dic);
		    }
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_cupones));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrCupones.size();i++){
		    	dic = arrCupones.get(i);
		    	dic.put("tipoF","Cupones");
		    	arrGeneral.add(dic);
		    }
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_ps));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrEventos.size();i++){
		    	dic = arrEventos.get(i);
		    	dic.put("tipoF","Eventos");
		    	arrGeneral.add(dic);
		    }
		    
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_noticias));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrNoticias.size();i++){
		    	dic = arrNoticias.get(i);
		    	dic.put("tipoF","Noticias");
		    	arrGeneral.add(dic);
		    }
		    
		    return null;
		}
	}

	public void filtros(View v){
	    filtro = Integer.parseInt(v.getTag().toString());
	    
	    Button btnTodos = (Button) findViewById(id.fav_btn_todos);
	    Button btnPat = (Button) findViewById(id.fav_btn_patrocinadores);
	    Button btnExp = (Button) findViewById(id.fav_btn_expositores);
	    Button btnNen = (Button) findViewById(id.fav_btn_programa);
	    Button btnPon = (Button) findViewById(id.fav_btn_ponentes);
	    Button btnCup = (Button) findViewById(id.fav_btn_cupones);
	    Button btnNot = (Button) findViewById(id.fav_btn_noticias);
	    Button btnEve = (Button) findViewById(id.fav_btn_eventos);
	    
	    btnTodos.setBackgroundResource(R.drawable.fav_todos_des);
	    btnPat.setBackgroundResource(R.drawable.fav_fondo_des);
	    btnExp.setBackgroundResource(R.drawable.fav_fondo_des);
	    btnNen.setBackgroundResource(R.drawable.fav_fondo_des);
	    btnPon.setBackgroundResource(R.drawable.fav_fondo_des);
	    btnCup.setBackgroundResource(R.drawable.fav_fondo_des);
	    btnNot.setBackgroundResource(R.drawable.fav_fondo_des);
	    btnEve.setBackgroundResource(R.drawable.fav_fondo_des);

	    if (filtro==0) {
	        v.setBackgroundResource(R.drawable.fav_todos_act);
	    }else{
	    	v.setBackgroundResource(R.drawable.fav_fondo_act);
	    }
	    
	    Refresh();
	}
	
	private void Refresh(){
		arrGeneral = new ArrayList<Dictionary<String,String>>();
		Dictionary<String, String> dic = null;
		
		if(filtro == 0 || filtro == 1){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_patrocinios));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrPatrocinadores.size();i++){
		    	dic = arrPatrocinadores.get(i);
		    	dic.put("tipoF","Patrocinadores");
		    	arrGeneral.add(dic);
		    }
		}
		
		if(filtro == 0 || filtro == 2){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_expositores));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrExpositores.size();i++){
		    	dic = arrExpositores.get(i);
		    	dic.put("tipoF","Expositores");
		    	arrGeneral.add(dic);
		    }
		}
		
		if(filtro == 0 || filtro == 3){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_programa));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrPonencias.size();i++){
		    	dic = arrPonencias.get(i);
		    	dic.put("tipoF","Ponencias");
		    	arrGeneral.add(dic);
		    }
		}
		
		if(filtro == 0 || filtro == 4){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_ponentes));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrPonentes.size();i++){
		    	dic = arrPonentes.get(i);
		    	dic.put("tipoF","Ponentes");
		    	arrGeneral.add(dic);
		    }
		}

		if(filtro == 0 || filtro == 5){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_cupones));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrCupones.size();i++){
		    	dic = arrCupones.get(i);
		    	dic.put("tipoF","Cupones");
		    	arrGeneral.add(dic);
		    }
		}
		
		if(filtro == 0 || filtro == 6){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_ps));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrEventos.size();i++){
		    	dic = arrEventos.get(i);
		    	dic.put("tipoF","Eventos");
		    	arrGeneral.add(dic);
		    }
		}
		
		if(filtro == 0 || filtro == 7){
		    dic = new Hashtable<String, String>();
		    dic.put("Header", getResources().getString(R.string.modulo_noticias));
		    arrGeneral.add(dic);
		    for(int i=0;i<arrNoticias.size();i++){
		    	dic = arrNoticias.get(i);
		    	dic.put("tipoF","Noticias");
		    	arrGeneral.add(dic);
		    }
		}
		
		
		Resources res = getResources();
		ListView listView = (ListView) findViewById(id.list);
		FavoritosArrayAdapter adapter = new FavoritosArrayAdapter(Favoritos.this, arrGeneral, res.getString(R.string.QRY_SERVLET));
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				muestra_detalle(position);
			}
		});
	}
	
	private void muestra_detalle(int x){
		Dictionary<String,String> dic = arrGeneral.get(x);
		String tipo = dic.get("tipoF");
		String idr = "";
		String nom = "";
		
		if(tipo == null){
			return;
		}else if(tipo.equals("Patrocinadores")){
			intent = new Intent(this, PatrocinadoresDetalle.class);
			idr = dic.get("pat_idr").toString();
			nom = "idpatrocinador";
        }else if(tipo.equals("Expositores")){
        	intent = new Intent(this, ExpositoresDetalle.class);
			idr = dic.get("exp_idr").toString();
			nom = "idexpositor";
        }else if(tipo.equals("Ponencias")){
        	intent = new Intent(this, ProgramaDetalle.class);
			idr = dic.get("nen_idr").toString();
			nom = "idponencia";
        }else if(tipo.equals("Ponentes")){
        	intent = new Intent(this, PonentesDetalle.class);
			idr = dic.get("pon_idr").toString();
			nom = "idponente";
        }else if(tipo.equals("Cupones")){
        	intent = new Intent(this, Cupon_Detalle.class);
			idr = dic.get("cup_idr").toString();
			nom = "idcupon";
        }else if(tipo.equals("Eventos")){
        	intent = new Intent(this, ProgramaSocDetalle.class);
			idr = dic.get("evs_idr").toString();
			nom = "idevs";
        }else if(tipo.equals("Noticias")){
        	intent = new Intent(this, NoticiasDetalle.class);
			idr = dic.get("not_idr").toString();
			nom = "not_idr";
		}
			
		Bundle bund = new Bundle();
		bund.putString(nom,idr);
		if(tipo.equals("Noticias")){
			bund.putString("sec_idr",dic.get("sec_idr").toString());	
		}
		intent.putExtras(bund);
		startActivity(intent);
	}
}
